<?php
/**
 * Created by PhpStorm.
 * User: phc
 * Date: 19-8-15
 * Time: 下午2:27
 */

namespace app\common\model;


class SlideshowModel extends CommonModel
{

    protected $table = "zbc_slideshow";

    public function getSlideShowByRemarks($remarks = "主页")
    {
        $result = db($this->table)
            ->alias('s')
            ->leftJoin(['zbc_images' => 'i'], 's.id = i.tableid ')
            ->where([
                'i.tablename' => $this->table,
                'i.enable' => 1,
                's.enable' => 1,
                's.remarks' => $remarks
            ])
            ->order(['s.createtime' => 'asc'])
            ->field('s.*,i.phy,i.other')
            ->select();

        return $result;
    }

    public function getSlideShows($size = 10, $page = 1)
    {
        $result = db($this->table)
            ->alias('s')
            ->leftJoin(['zbc_images' => 'i'], 's.id = i.tableid ')
            ->where([
                'i.tablename' => $this->table,
                'i.enable' => 1,
                's.enable' => 1
            ])
            ->order(['s.createtime' => 'asc'])
            ->limit($size * ($page - 1), $size)
            ->field('s.*,i.phy,i.other,i.id imgid')
            ->select();

        return $result;
    }

    public function getSlideShowCount()
    {
        $result = $this->getCount($this->table);
        return $result;
    }

    public function addSlideShow($param, $phy)
    {
        $this->startTrans();
        try {
            $id = $this->add($this->table, $param);
            if (!$id)
                throw Exception('保存信息错误');
            if (!$this->addImages($this->table, $phy, $id))
                throw Exception('保存图片错误');
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->rollback();
            return false;
        }

    }

    public function getSlideShowById($id)
    {
        $result["data"] = $this->getById($this->table, $id);
        $result["image"] = $this->getImagesByTableId($this->table, $result["data"]["id"]);
        return $result;
    }

    public function updateSlideShowById($param, $phy)
    {
        $this->startTrans();
        try {
            $id = $param["id"];
            if (!$id || !$phy)
                throw Exception('参数缺失');
            if ($this->updateById($this->table, $param) === false)
                throw Exception('修改信息错误');
            $imgInfo = $this->getImagesByTableId($this->table, $id);
            $imgResult = false;
            if (!$imgInfo) {
                //插入图片
                $imgResult = $this->addImages($this->table, $phy, $id);
            } else {
                //修改
                $imgInfo[0]['phy'] = $phy;
                $imgInfo[0]['updatetime'] = date("Y-m-d H:i:s");
                $imgResult = $this->updateById('zbc_images', $imgInfo[0]);
            }
            if ($imgResult === false)
                throw Exception('保存图片错误');
            // 提交事务
            $this->commit();
            return true;
        } catch (\Exception $e) {
            // 回滚事务
            $this->rollback();
            return false;
        }
    }

    public function deleteSlideshow($arrayId)
    {
        $this->startTrans();
        try {
            if ($this->deleteByIds($this->table, $arrayId) === false)
                throw Exception('删除信息错误');
            if ($this->deleteImagesByTableId($this->table, $arrayId) === false)
                throw Exception('删除图片错误');
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->rollback();
            return false;
        }
    }
}